﻿using System;
using System.Data;
using System.Collections.Generic;
using VSA.Common;
using VSA.Entity;
using VSA.DAL;

namespace VSA.BLL
{
    public static partial class CompanyPersonMappingBLL
    {
        private static readonly CompanyPersonMappingDAL dal = new CompanyPersonMappingDAL();

        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public static int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(int CompanyId, int PersonId)
        {
            return dal.Exists(CompanyId, PersonId);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(CompanyPersonMapping model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(CompanyPersonMapping model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(int CompanyId, int PersonId)
        {

            dal.Delete(CompanyId, PersonId);
        }

        public static void Delete(int PersonId)
        {

            dal.Delete(PersonId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static CompanyPersonMapping GetModel(int CompanyId, int PersonId)
        {
            return dal.GetModel(CompanyId, PersonId);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public static CompanyPersonMapping GetModelByCache(int CompanyId, int PersonId)
        {

            string CacheKey = "t_CompanyPersonMappingModel-" + CompanyId + PersonId;
            object objModel = VSA.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(CompanyId, PersonId);
                    if (objModel != null)
                    {
                        int ModelCache = VSA.Common.ConfigHelper.GetConfigInt("ModelCache");
                        VSA.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (CompanyPersonMapping)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static List<CompanyPersonMapping> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static List<CompanyPersonMapping> DataTableToList(DataTable dt)
        {
            List<CompanyPersonMapping> modelList = new List<CompanyPersonMapping>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CompanyPersonMapping model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new CompanyPersonMapping();
                    if (dt.Rows[n]["CompanyId"].ToString() != "")
                    {
                        model.CompanyId = int.Parse(dt.Rows[n]["CompanyId"].ToString());
                    }
                    if (dt.Rows[n]["PersonId"].ToString() != "")
                    {
                        model.PersonId = int.Parse(dt.Rows[n]["PersonId"].ToString());
                    }
                    if (dt.Rows[n]["CreateDate"].ToString() != "")
                    {
                        model.CreateDate = DateTime.Parse(dt.Rows[n]["CreateDate"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}
